# coding=utf-8
import allure
from common.utils import Utils
from common.assert_operator import *
from common.db import *
from common.read_yaml import *
from common.verification import Verification

string_assert = string_assert() # 字符串校验类
pact = Pact() # 契约校验类
execute_sql = sql() # sql集合
utils = Utils()
verification = Verification()
data = ReadYaml_By_Root_Dict("case/02_approve_submit_test/02_3_FinishNode_Confirm.yml").get_yaml_data()  # 读取数据

@allure.feature('审批提交，明确路径')  # 测试报告显示测试功能
class Test_02_3_FinishNode_Confirm():

    # 获取yml文件中[审批明确路径_不可撤回]的数据参数
    approve_process_params = data['审批明确路径_不可撤回']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(approve_process_params,"$[*][0]")
    @pytest.mark.parametrize("title, param, expect", approve_process_params,ids=ids_list)
    def test_FinishNode_Confirm(self, user_login, title, param, expect):
        Logger.info("*************** {}开始执行用例 ***************".format(title))
        allure.dynamic.description(f"{param['description']}")  # # 动态解析用例描述在测试报告中展示
        user,process_name = param['user'],param['process_name']
        DBNodeVerify, MessageVerify = '数据库校验', '消息校验'
        contract_format = pact.approve_pact_format()    # 审批响应契约格式
        main = user_login.goto_Approve_Process(user)    # 登录到审批类实例
        process_instance_resp = main.open_process_instance(process_name)   # 通过流程名称打开流程实例
        process_params = main.get_key_by_process_instance(process_instance_resp,'params')['params']  # 打开流程响应的Params
        proc_id = jsonpath.jsonpath(process_params,'$.proc_id')[0] # 当前流程的proc_id
        node_id = jsonpath.jsonpath(process_params,'$.node_id')[0] # 当前流程的node_id
        package_id = jsonpath.jsonpath(process_params,'$.package_id')[0] # 当前流程的package_id
        response = main.approve_submit(param, process_instance_resp)    # 提交审批
        string_assert.eq(response.status_code, 200)
        verification.after_end(process_name,response,expect,contract_format,DBNodeVerify,MessageVerify)    # 审批归档后校验
        Logger.info("*************** 【{}】审批撤回 ***************".format(process_name))
        # 使用已审批的节点信息打开流程
        process_instance = main.open_process_instance(node_id=node_id, proc_id=proc_id, package_id=package_id)
        # 撤回审批
        withdraw_resp = main.approval_withdraw(process_instance, param['process_name'], '审批撤回')
        dict_assert(expect['撤回异常响应'], withdraw_resp.json())
        Logger.info("*************** {}结束执行用例 ***************".format(title))

    # 获取yml文件中[审批明确路径_调整后确认路径]的数据参数
    approve_process_params = data['审批明确路径_调整后确认路径']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(approve_process_params,"$[*][0]")
    @pytest.mark.parametrize("title, param, expect", approve_process_params,ids=ids_list)
    def test_FinishNode_Confirm_Open_Path(self, user_login, title, param, expect):
        Logger.info("*************** {}开始执行用例 ***************".format(title))
        allure.dynamic.description(f"{param['description']}")  # # 动态解析用例描述在测试报告中展示
        user,process_name = param['user'],param['process_name']
        DBNodeVerify, MessageVerify,ConfirmPath = '数据库校验', '消息校验','确认路径校验'
        main = user_login.goto_Approve_Process(user)    # 登录到审批类实例
        contract_format = pact.approve_pact_format()
        process_instance_resp = main.open_process_instance(process_name)   # 通过流程名称打开流程实例
        process_params = main.get_key_by_process_instance(process_instance_resp,'params')['params']  # 打开流程响应的Params
        proc_id = jsonpath.jsonpath(process_params,'$.proc_id')[0] # 当前流程的proc_id
        node_id = jsonpath.jsonpath(process_params,'$.node_id')[0] # 当前流程的node_id
        package_id = jsonpath.jsonpath(process_params,'$.package_id')[0] # 当前流程的package_id
        confirm_path_resp= main.approve_confirm_path(param, process_instance_resp)  # 确认路径
        response = main.approve_submit(param, process_instance_resp)    # 提交审批
        # 审批后校验
        verification.after_approve(process_name, response, expect, contract_format, DBNodeVerify, MessageVerify,
                                   confirm_path_resp=confirm_path_resp, confirm_path_verify=ConfirmPath)
        # 使用已审批的节点信息打开流程
        process_instance = main.open_process_instance(node_id=node_id, proc_id=proc_id, package_id=package_id)
        # 撤回审批
        withdraw_resp = main.approval_withdraw(process_instance, param['process_name'], '审批撤回')
        # 审批撤回契约校验
        contract_format = pact.approval_withdraw_pact_format()
        pact.pact_verify(contract_format, withdraw_resp.json())
        Logger.info("*************** {}结束执行用例 ***************".format(title))
